package com.example.littleSpider.myapplication.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.littleSpider.myapplication.R;
import com.example.littleSpider.myapplication.myInterfaces.MethodInTitle;
import com.littlespider.mylibrary.util.T;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 很多界面都共用的标题栏布局,我抽成了一个view
 * 默认显示返回版的左键
 * Created by aduser on 2017/12/9.
 */

public class TitleView extends RelativeLayout implements MethodInTitle {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.rl_right_two)
    RelativeLayout rl_right_two;
    @BindView(R.id.iv_title)
    ImageView iv_title;
    @BindView(R.id.ll_title)
    LinearLayout ll_title;
    @BindView(R.id.tv_city)
    TextView tv_city;
    @BindView(R.id.tv_back)
    TextView tv_back;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.iv_city)
    ImageView iv_city;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.ll_city)
    LinearLayout ll_city;
    @BindView(R.id.iv_title_text)
    ImageView iv_title_text;
    @BindView(R.id.iv_right_one)
    ImageView iv_right_one;
    @BindView(R.id.iv_right_two)
    ImageView iv_right_two;
    @BindView(R.id.tv_right_one)
    TextView tv_right_one;
    @BindView(R.id.tv_right_two)
    TextView tv_right_two;

    Context mContext;
    MethodInTitle methodInTitle;

    public TitleView(Context context) {
        super(context);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.include_title_view, this);
        ButterKnife.bind(view);
        methodInTitle = TitleView.this;
        TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.TitleView);
        String s_name = typedArray.getString(R.styleable.TitleView_title);
        Drawable right_one = typedArray.getDrawable(R.styleable.TitleView_right_one);
        String st_right_one = typedArray.getString(R.styleable.TitleView_right_one_text);
        String st_right_two = typedArray.getString(R.styleable.TitleView_right_two_text);
        Drawable right_two = typedArray.getDrawable(R.styleable.TitleView_right_two);
        if (right_one != null){
            iv_right_one.setImageDrawable(right_one);
        }
        if (right_two != null){
            iv_right_two.setImageDrawable(right_two);
        }
        if (st_right_one != null){
            tv_right_one.setText(st_right_one);
        }
        if (st_right_two != null){
            tv_right_two.setText(st_right_two);
        }
    }

    /**
     * 设置标题
     * @param title 标题文字内容
     * @param methodInTitle 标题栏的方法接口
     */
    public void initMyTitle(String title, MethodInTitle methodInTitle){
        initMyTitle(title, methodInTitle, true);
    }

    /**
     * 设置标题
     * @param title 同上
     * @param methodInTitle 同上
     * @param isShowRightTwo 是否显示右边第二个按钮
     */
    public void initMyTitle(String title, MethodInTitle methodInTitle, boolean isShowRightTwo){
        changeTitleText(title);
        this.methodInTitle = methodInTitle;
        showRightTwo(isShowRightTwo);
    }

    @OnClick(R.id.rl_back)
    public void goBack(){
        methodInTitle.setGoBack();
    }

    @OnClick(R.id.tv_title)
    public void clickOnTitle(){
        methodInTitle.onTitleText();
    }

    @OnClick(R.id.rl_right_two)
    public void clickOnRightTwo(){
//        Toasty.info(mContext, "右边第二个按钮").show();
        methodInTitle.onRightTwo();
    }

    @OnClick(R.id.rl_right_one)
    public void clickOnRightOne(){
//        Toasty.info(mContext, "右边第一个按钮").show();
        methodInTitle.onRightOne();
    }

    public void showRightTwo(boolean isShow){
        if(isShow){
            rl_right_two.setVisibility(View.VISIBLE);
        }else {
            rl_right_two.setVisibility(View.GONE);
        }
    }

    /**
     * 改变标题内容
     * @param titleName
     */
    public void changeTitleText(String titleName){
        tv_title.setText(titleName);
    }

    /**
     * 隐藏标题旁的小图标
     */
    public void hideTitleTextImage(){
        iv_title_text.setVisibility(View.GONE);
    }

    /**
     * 显示标题旁的小图标
     */
    public void showTitleTextImage(){
        iv_title_text.setVisibility(View.VISIBLE);
    }

    /**
     * 显示代替标题文字内容的图片
     * @param imageRes 图片的资源文件
     */
    public void showImage(int imageRes){
        iv_title.setVisibility(View.VISIBLE);
        iv_title.setImageResource(imageRes);
    }

    /**
     * 隐藏代替标题文字内容的图片
     */
    public void hideImage(){
        iv_title.setVisibility(View.GONE);
    }

    /**
     * 显示文字版的标题
     */
    public void showTitleText(){
        ll_title.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏文字版的标题
     */
    public void hideTitleText(){
        ll_title.setVisibility(View.GONE);
    }

    /**
     * 隐藏城市版的左键
     */
    public void hideCity(){
        ll_city.setVisibility(View.GONE);
    }

    /**
     * 显示城市版的左键
     */
    public void showCity(){
        ll_city.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏返回版的左键
     */
    public void hideBack(){
        ll_back.setVisibility(View.GONE);
    }

    /**
     * 显示返回版的左键
     */
    public void showBack(){
        ll_back.setVisibility(View.VISIBLE);
    }

    @Override
    public void setGoBack() {
        T.show(mContext, "返回");
    }

    @Override
    public void onRightOne() {
        T.show(mContext, "rightOne");
    }

    @Override
    public void onRightTwo() {
        T.show(mContext, "rightTwo");
    }

    @Override
    public void onTitleText() {
        T.show(mContext, "请不要点击标题");
//        Toasty.info(mContext, "请不要点击标题").show();
    }
}
